package com.numberone.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.numberone.entity.Check;
import com.numberone.entity.PageInfo;
import com.numberone.entity.User;
import com.numberone.mapper.CheckMapper;
import com.numberone.service.ICheckService;
import com.numberone.service.IUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zzc
 * @since 2019-04-12
 */
@Controller
public class UserController {
    private static Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private IUserService userService;
    @Autowired
    private ICheckService checkService;
    @Autowired
    private CheckMapper checkMapper;

    @RequestMapping(value = "/login.shtml")
    public String login(@RequestParam String username, @RequestParam String password, HttpSession session) {

        User user = userService.getOne(new QueryWrapper<User>().eq("accountName", username));
        System.out.println(user);
        logger.info("登录用户：" + username + " 密码：" + password);
        if (user.getPassword().equals(password)) {
            session.setAttribute("username", username);
            return "index";
        } else {
            return "/";
        }
    }

    @ResponseBody
    @RequestMapping(value = "/check/findByPage.shtml", method = RequestMethod.GET)
    public Object findByPage(PageInfo pageInfo) {
        Page<Check> page = new Page<>(pageInfo.getPagenumber(), pageInfo.getPagesize());
        IPage<Check> checks = checkService.page(page);
        return checks;
    }

    @ResponseBody
    @RequestMapping(value = "/check/findByPageByAge.shtml", method = RequestMethod.GET)
    public Object findByPageByAge(PageInfo pageInfo) {
        Page<Check> page = new Page<>(pageInfo.getPagenumber(), pageInfo.getPagesize());
        page.setAsc("age");
        IPage<Check> checks = checkService.page(page);
        return checks;
    }

    @ResponseBody
    @RequestMapping(value = "/check/findByPageByBMI.shtml", method = RequestMethod.GET)
    public Object findByPageByBMI(PageInfo pageInfo) {
        Page<Check> page = new Page<>(pageInfo.getPagenumber(), pageInfo.getPagesize());
        page.setAsc("bmi");
        IPage<Check> checks = checkService.page(page);
        return checks;
    }

    @ResponseBody
    @RequestMapping(value = "/check/findByPageByBloodSugar.shtml", method = RequestMethod.GET)
    public Object findByPageByBloodSugar(PageInfo pageInfo) {
        Page<Check> page = new Page<>(pageInfo.getPagenumber(), pageInfo.getPagesize());
        page.setAsc("blood_sugar");
        IPage<Check> checks = checkService.page(page);
        return checks;
    }

    @ResponseBody
    @RequestMapping(value = "/check/findByPageBySmoke.shtml", method = RequestMethod.GET)
    public Object findByPageBySmoke(PageInfo pageInfo) {
        Page<Check> page = new Page<>(pageInfo.getPagenumber(), pageInfo.getPagesize());
        page.setAsc("smoke");
        IPage<Check> checks = checkService.page(page);
        return checks;
    }

    @ResponseBody
    @RequestMapping(value = "/check/findByPageByTime.shtml", method = RequestMethod.GET)
    public Object findByPageByTime(PageInfo pageInfo) {
        Page<Check> page = new Page<>(pageInfo.getPagenumber(), pageInfo.getPagesize());
        page.setAsc("update_time");
        IPage<Check> checks = checkService.page(page);
        return checks;
    }

    @ResponseBody
    @RequestMapping(value = "/check/findByPageByDrink.shtml", method = RequestMethod.GET)
    public Object findByPageByDrink(PageInfo pageInfo) {
        Page<Check> page = new Page<>(pageInfo.getPagenumber(), pageInfo.getPagesize());
        page.setAsc("drink");
        IPage<Check> checks = checkService.page(page);
        return checks;
    }

    @ResponseBody
    @RequestMapping(value = "/check/Select.shtml", method = RequestMethod.GET)
    public Object Select(PageInfo pageInfo) {
        return checkService.find(pageInfo.getName(), pageInfo.getValue());
    }

    @RequestMapping(value = "/changePwd.shtml", method = RequestMethod.POST)
    public String changePwd(@RequestParam String username, @RequestParam String oldPwd, @RequestParam String newPwd,HttpSession session) {
        User user = userService.getOne(new QueryWrapper<User>().eq("accountName", username));
        System.out.println(user);
        logger.info("登录用户：" + username + " 密码：" + oldPwd);
        if (user.getPassword().equals(oldPwd)) {
            session.setAttribute("username", username);
            UpdateWrapper<User> updateWrapper=new UpdateWrapper<>();
            updateWrapper.set("password",newPwd);
            if (userService.update(user,updateWrapper.eq("id",user.getId()))){
                return "index";
            }else {
                return "error";
            }
        }else {
            return "error";
        }

    }

    @ResponseBody
    @RequestMapping(value = "/check/dataTransfer.shtml", method = RequestMethod.GET)
    public Object dataTransfer(PageInfo pageInfo) {
        Page<Check> page = new Page<>(pageInfo.getPagenumber(), pageInfo.getPagesize());
        IPage<Check> checks = checkService.page(page);
        Connection connection=DBUtil.getConnection();
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(
                    " if not exists (select id from [guest].[ly_check] where id = ?)" +
                    "INSERT INTO [guest].[ly_check] VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)  " +
                    "else update [guest].[ly_check] set age=? and sex=? and nxgbhistory=? and press_radio=? and " +
                            "press_med_radio=? and press_med=? and heart_radio=? and heart__med_radio=? and " +
                            "heart_med=? and blood_sugar=? and blood_sugar_2h=? and triglyceride=? and lipoprotein=? and " +
                            "txbgas=? and jdmcs_check=? and jdmbk_check=? and jdmxzl=? and jdmCTA_check=? and " +
                            "jdmCTAbk_check=? and jdmCTAxzl=? and jldpl_check=? and lnxgxz_radio=? and smoke=? and " +
                            "drink=? and bmi=? and other=? and phone=? and update_time=? and xl=? and address=? and " +
                            "job=? and form=? WHERE id=?;");
            for (Check check:checks.getRecords()){
                preparedStatement.setString(1, check.getId());
                preparedStatement.setString(2,check.getId());
                preparedStatement.setString(3,check.getAge());
                preparedStatement.setString(4,check.getNxgbhistory());
                preparedStatement.setString(5,check.getPressRadio());
                preparedStatement.setString(2,check.getPressMedRadio());
                preparedStatement.setString(2,check.getPressMed());
                preparedStatement.setString(2,check.getHeartRadio());
                preparedStatement.setString(2,check.getHeartMedRadio());
                preparedStatement.setString(2,check.getHeartMed());
                preparedStatement.setString(2,check.getBloodSugar());
                preparedStatement.setString(2,check.getBloodSugar2h());
                //preparedStatement.setString(2,check.getTriglyceride());
                //preparedStatement.setString(2,check.getLipoprotein());
                preparedStatement.setString(2,check.getTxbgas());
                //preparedStatement.setString(2,check.getJdmcsCheck());
                preparedStatement.setString(2,check.getJdmbkCheck());
                preparedStatement.setString(2,check.getJdmxzl());
                //preparedStatement.setString(2,check.getJdmctaCheck());
                //preparedStatement.setString(2,check.getJdmctabkCheck());
                preparedStatement.setString(2,check.getJdmCTAxzl());
                //preparedStatement.setString(2,check.getJldplCheck());
                preparedStatement.setString(2,check.getLnxgxzRadio());
                preparedStatement.setString(2,check.getSmoke());
                preparedStatement.setString(2,check.getDrink());
                preparedStatement.setString(2,check.getBmi());
                preparedStatement.setString(2,check.getOther());
                preparedStatement.setString(2,check.getPhone());
                preparedStatement.setString(2,check.getUpdateTime());
                preparedStatement.setString(2,check.getXl());
                preparedStatement.setString(2,check.getAddress());
                preparedStatement.setString(2,check.getJob());
                preparedStatement.setString(2,"微信");
                preparedStatement.addBatch();
            }
            for(int i=10;i<20;i++){
            preparedStatement.setString(1, "白白");
            preparedStatement.setInt(2,i);
            preparedStatement.addBatch();
        }
            preparedStatement.addBatch();
            preparedStatement.executeBatch();
            connection.commit();
            preparedStatement.clearBatch();
        } catch (SQLException e) {
            System.out.println("执行失败");
            e.printStackTrace();
        }
        return checkService.find(pageInfo.getName(), pageInfo.getValue());
    }

    @RequestMapping("login1.shtml")
    public String login1(HttpSession session) {
        String username = (String) session.getAttribute("username");
        if (username != null) {
            return "index";
        }
        return "/";
    }

    /*@RequestMapping("dataTransfer.html")
    public*/


}
